package com.firefightingiot.firefightingiot.web.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.firefightingiot.firefightingiot.common.base.BaseService;
import com.firefightingiot.firefightingiot.model.DeviceAlarmEvent;
import com.firefightingiot.firefightingiot.model.DeviceAlarmLog;
import com.firefightingiot.firefightingiot.model.DeviceBusiness;
import com.firefightingiot.firefightingiot.support.mybatisplus.QueryWrapper;
import com.firefightingiot.firefightingiot.support.utils.hikvision.HikvisionInterfaceCall;
import com.firefightingiot.firefightingiot.web.mapper.DeviceAlarmEventMapper;
import com.firefightingiot.firefightingiot.web.mapper.DeviceAlarmLogMapper;
import com.firefightingiot.firefightingiot.web.mapper.DeviceBusinessMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: YangYu
 * @ClassName: AsyncService
 * @Date: Created in 14:15 2021/12/8
 * @Description: 异步处理服务
 */
@Service
public class AsyncService extends BaseService {

    /**
     * @Author YangYu
     * @Description 海康关门异步处理
     * @Date Created in 14:17 2021/12/8
     * @Param [jsonParam]
     */

    @Async
    public void remoteClosing(JSONObject jsonParam){
        HikvisionInterfaceCall.eventSubscriptionsHK("/api/edp/v1/setAlarmOut",jsonParam);
    }

    @Resource
    private DeviceAlarmLogMapper deviceAlarmLogMapper;
    @Resource
    FireService fireService;
    @Resource
    private DeviceBusinessMapper deviceBusinessMapper;
    @Resource
    private DeviceAlarmEventMapper deviceAlarmEventMapper;

    @Async
    public void handle(JSONObject jsonParam) throws InterruptedException, ParseException {
        List<DeviceAlarmLog> deviceAlarmLogList = deviceAlarmLogMapper.selectList(new QueryWrapper<DeviceAlarmLog>().eq("is_handle",0));

        for (DeviceAlarmLog deviceAlarmLog : deviceAlarmLogList){
            jsonParam.put("number",deviceAlarmLog.getNumber());
            deviceAlarmLogMapper.updateByNumber(jsonParam);//根据number修改日志为已处理
            //更改设备状态为正常
            DeviceBusiness alarmDeviceBusiness = new DeviceBusiness();
            alarmDeviceBusiness.setCurrentstate("1");
            deviceBusinessMapper.update(alarmDeviceBusiness, new QueryWrapper<DeviceBusiness>().eq("number", deviceAlarmLog.getNumber()));
        }
    }

    /**
     * @Author YangYu
     * @Description 下发
     * @Date Created in 09:55 2023/7/31
     */
    @Async
    public void addBook(String number,Integer type,Integer pressKey,String phone,String productId,String name) throws Exception {
        JSONObject jsonParam = new JSONObject();
        if (productId.equals("5003")){
            jsonParam.put("topic","beadHouse/" +number+"/receive/");
            if (type == 1){
                jsonParam.put("mqtt_content","【全芯物联SOS】亲情号,id:"+number+",号码"+pressKey+":"+phone);
            }else {
                jsonParam.put("mqtt_content","【全芯物联SOS】紧急号,id:"+number+",号码"+pressKey+":"+phone);
            }
            rest(jsonParam, "mqtt_down", "mqtt_down", "mq", "POST");
        }else {
            jsonParam.put("number",number);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("mobile",phone);
            jsonObject.put("nickName",name);
            JSONArray jsonArray = new JSONArray();
            if (type == 1){
                jsonObject.put("shortcut",pressKey);
                jsonArray.add(jsonObject);
                jsonParam.put("param","loveArray");
                jsonParam.put("array",jsonArray);
                post_x_www_form_urlencoded(jsonParam,"mg_api","setLoveNum","mg_api");
            }else {
                jsonArray.add(jsonObject);
                jsonParam.put("array",jsonArray);
                jsonParam.put("param","sosArray");
                post_x_www_form_urlencoded(jsonParam,"mg_api","setSosNum","mg_api");
            }
        }
    }


    public static String getTime(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        String y = format.format(date);
        format = new SimpleDateFormat("MM");
        String m = format.format(date);
        format = new SimpleDateFormat("dd");
        String d = format.format(date);
        format = new SimpleDateFormat("HH:mm");
        String h = format.format(date);
        return y+"年"+m+"月"+d+"日"+h+"分";
    }

    /**
     * @Author YangYu
     * @Description 异步批量添加事件
     * @Date Created in 16:01 2024/4/15
     * @Param [number, time]
     */
    @Async
    public void setAlarmEvent(String number,String time){
        QueryWrapper<DeviceAlarmLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id").eq("number",number).eq("is_handle", 0);

        List<DeviceAlarmLog> deviceAlarmLogList = deviceAlarmLogMapper.selectList(queryWrapper);
        List<DeviceAlarmEvent> deviceAlarmEventList = new ArrayList<>();
        for (DeviceAlarmLog deviceAlarmLog : deviceAlarmLogList){
            DeviceAlarmEvent deviceAlarmEvent = new DeviceAlarmEvent();
            deviceAlarmEvent.setDeviceAlarmLogId(deviceAlarmLog.getId());
            deviceAlarmEvent.setTime(time);
            deviceAlarmEvent.setEventName("事件解除");
            deviceAlarmEventList.add(deviceAlarmEvent);
        }
        deviceAlarmEventMapper.insertDeviceAlarmEventListZB(deviceAlarmEventList);
    }
}
